package com.sc.test.menutest;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.sc.test.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * description:
 * author: ShenChao
 * time: 2019-09-19
 * update:
 */
public class MenuTestActivity extends AppCompatActivity {

    @BindView(R.id.rlv_my)
    RecyclerView rlvMy;
    @BindView(R.id.rlv_all)
    RecyclerView rlvAll;
    ArrayList<DataBean> myData = new ArrayList<>();
    ArrayList<DataBean> allData = new ArrayList<>();

    private RecyclerView.LayoutManager mLayoutManager, aLayoutManager;
    private DragRvAdapter mAdapter, aAdapter;
    private ItemTouchHelper mItemTouchHelper, aItemTouchHelper;
    private ItemTouchHelper.Callback mCallback, aCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_test);
        ButterKnife.bind(this);
        initView();
    }

    protected void initView() {
        String temp = "条目";
        for (int i = 0; i < 30; i++) {
            DataBean dataBean = new DataBean();
            dataBean.text = temp + i;
            dataBean.showClose = false;
            if (i % 5 == 0) {
                myData.add(dataBean);
            } else {
                allData.add(dataBean);
            }

        }
        initAction();
    }

    private void initAction() {
        mCallback = new ItemTouchHelper.Callback() {
            /**
             * 设置滑动类型标记
             *
             * @param recyclerView
             * @param viewHolder
             * @return
             *          返回一个整数类型的标识，用于判断Item那种移动行为是允许的
             */
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                return makeMovementFlags(
                        ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT, 0);
            }

            /**
             * Item是否支持长按拖动
             *
             * @return
             *          true  支持长按操作
             *          false 不支持长按操作
             */
            @Override
            public boolean isLongPressDragEnabled() {
                return true;
            }

            /**
             * Item是否支持滑动
             *
             * @return
             *          true  支持滑动操作
             *          false 不支持滑动操作
             */
            @Override
            public boolean isItemViewSwipeEnabled() {
                return false;
            }

            /**
             * 拖拽切换Item的回调
             *
             * @param recyclerView
             * @param viewHolder
             * @param target
             * @return
             *          如果Item切换了位置，返回true；反之，返回false
             */
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                mAdapter.move(viewHolder.getAdapterPosition(), target.getAdapterPosition());
                return true;
            }

            /**
             * 滑动item
             * @param viewHolder
             * @param direction
             */
            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

            }

            /**
             * Item被选中时候回调
             *
             * @param viewHolder
             * @param actionState
             *          当前Item的状态
             *          ItemTouchHelper.ACTION_STATE_IDLE   闲置状态
             *          ItemTouchHelper.ACTION_STATE_SWIPE  滑动中状态
             *          ItemTouchHelper#ACTION_STATE_DRAG   拖拽中状态
             */
            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
//                if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
//                    viewHolder.itemView.setBackgroundResource(R.color.gray);
//                }
                super.onSelectedChanged(viewHolder, actionState);
            }

            /**
             * 用户操作完毕或者动画完毕后会被调用
             *
             * @param recyclerView
             * @param viewHolder
             */
            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                // 操作完毕后恢复颜色
                viewHolder.itemView.setBackgroundResource(android.R.color.transparent);
                super.clearView(recyclerView, viewHolder);
            }
        };
        aCallback = new ItemTouchHelper.Callback() {
            /**
             * 设置滑动类型标记
             *
             * @param recyclerView
             * @param viewHolder
             * @return
             *          返回一个整数类型的标识，用于判断Item那种移动行为是允许的
             */
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                return makeMovementFlags(
                        ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT, 0);
            }

            /**
             * Item是否支持长按拖动
             *
             * @return
             *          true  支持长按操作
             *          false 不支持长按操作
             */
            @Override
            public boolean isLongPressDragEnabled() {
                return true;
            }

            /**
             * Item是否支持滑动
             *
             * @return
             *          true  支持滑动操作
             *          false 不支持滑动操作
             */
            @Override
            public boolean isItemViewSwipeEnabled() {
                return false;
            }

            /**
             * 拖拽切换Item的回调
             *
             * @param recyclerView
             * @param viewHolder
             * @param target
             * @return
             *          如果Item切换了位置，返回true；反之，返回false
             */
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                mAdapter.move(viewHolder.getAdapterPosition(), target.getAdapterPosition());
                return true;
            }

            /**
             * 滑动item
             * @param viewHolder
             * @param direction
             */
            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

            }

            /**
             * Item被选中时候回调
             *
             * @param viewHolder
             * @param actionState
             *          当前Item的状态
             *          ItemTouchHelper.ACTION_STATE_IDLE   闲置状态
             *          ItemTouchHelper.ACTION_STATE_SWIPE  滑动中状态
             *          ItemTouchHelper#ACTION_STATE_DRAG   拖拽中状态
             */
            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
//                if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
//                    viewHolder.itemView.setBackgroundResource(R.color.gray);
//                }
                super.onSelectedChanged(viewHolder, actionState);
            }

            /**
             * 用户操作完毕或者动画完毕后会被调用
             *
             * @param recyclerView
             * @param viewHolder
             */
            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                // 操作完毕后恢复颜色
                viewHolder.itemView.setBackgroundResource(android.R.color.transparent);
                super.clearView(recyclerView, viewHolder);
            }
        };

        mItemTouchHelper = new ItemTouchHelper(mCallback);
        aItemTouchHelper = new ItemTouchHelper(aCallback);
        mLayoutManager = new GridLayoutManager(this, 4, RecyclerView.VERTICAL, false);
        aLayoutManager = new GridLayoutManager(this, 4, RecyclerView.VERTICAL, false);
        mAdapter = new DragRvAdapter(myData);
        mAdapter.setOnItemClickListener(new DragRvAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MenuTestActivity.this,"第" + (position) + "条被点击",Toast.LENGTH_SHORT).show();
                mAdapter.deleteItem(position);
                aAdapter.addNewItem(position);
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(MenuTestActivity.this, "第" + (position) + "条被长按", Toast.LENGTH_SHORT).show();
                rlvAll.setVisibility(View.VISIBLE);
                aAdapter.showClose();
            }
        });
        aAdapter = new DragRvAdapter(allData);
        aAdapter.setOnItemClickListener(new DragRvAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MenuTestActivity.this,"第" + (position) + "条被点击",Toast.LENGTH_SHORT).show();
                aAdapter.deleteItem(position);
                mAdapter.addNewItem(position);
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(MenuTestActivity.this,"第" + (position) + "条被长按",Toast.LENGTH_SHORT).show();
            }
        });
        rlvMy.setLayoutManager(mLayoutManager);
        rlvMy.setAdapter(mAdapter);
        rlvMy.setItemAnimator(new DefaultItemAnimator());

        rlvAll.setLayoutManager(aLayoutManager);
        rlvAll.setAdapter(aAdapter);
        rlvAll.setItemAnimator(new DefaultItemAnimator());

        mItemTouchHelper.attachToRecyclerView(rlvMy);
        aItemTouchHelper.attachToRecyclerView(rlvAll);
    }
}
